package com.yanqu.road.server.cmd.activity.tanghulu;

import com.yanqu.road.pb.activity.TangHuLuProto;
import com.yanqu.road.server.cmd.base.AbstractCommand;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.gameplayer.module.activity.tanghulu.TangHuLuModule;
import com.yanqu.road.server.manger.activity.tanghulu.TangHuLuMgr;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.server.protocol.GameErrorCode;
import com.yanqu.road.server.protocol.Protocol;

import java.util.ArrayList;
import java.util.List;

@Cmd(code = Protocol.S_TANGHULU_ACTIVITY_LAUNCH_FRUITS, desc = "水果弹射")
public class TangHuLuLaunchFruitsCmd extends AbstractCommand {

    int goodsId;
    int timeGap;
    int flyTime;
    int times;
    int type;
    List<TangHuLuProto.TangHuLuPanChangeSpeedTemp> speedList = new ArrayList<>();

    @Override
    public boolean parseData(YanQuMessage packet) throws Exception {
        TangHuLuProto.TangHuLuLaunchFruitsReqMsg reqMsg = TangHuLuProto.TangHuLuLaunchFruitsReqMsg.parseFrom(packet.getBytes());
        goodsId = reqMsg.getGoodsId();
        timeGap = reqMsg.getTimeGap();
        flyTime = reqMsg.getFlyTime();
        times = reqMsg.getTimes();
        speedList = reqMsg.getCurSpeedList();
        type = reqMsg.getType();
        return true;
    }

    @Override
    public void execute(GamePlayer player, YanQuMessage packet) throws Exception {
        if(TangHuLuMgr.getShowTimeActivityId() == 0){
            ret = GameErrorCode.E_ACTIVITY_NO_FOUND;
            return;
        }
        if(!TangHuLuMgr.activityInTime()){
            ret = GameErrorCode.E_ACTIVITY_OVER_TIME;
            return;
        }
        //判定水果道具是否有效
        if(goodsId != TangHuLuMgr.TANGHULU_CARAMBOLA_ID && goodsId != TangHuLuMgr.TANGHULU_HAWTHORN_ID &&
            goodsId != TangHuLuMgr.TANGHULU_KIWIFRUIT_ID && goodsId != TangHuLuMgr.TANGHULU_STRAWBERRY_ID){
            ret = GameErrorCode.E_COMMON_ACTIVITY_NO_ACTIVITY_GOODS;
            return;
        }

        ret = player.getModule(TangHuLuModule.class).launchFruits(goodsId, timeGap, flyTime, times, speedList, type);

    }

    @Override
    public void sendData(GamePlayer player) {
        if(ret != 0){
            TangHuLuProto.TangHuLuLaunchFruitsRespMsg.Builder respMsg = TangHuLuProto.TangHuLuLaunchFruitsRespMsg.newBuilder();
            respMsg.setRet(ret);
            respMsg.setTimes(times);
            player.sendPacket(Protocol.U_TANGHULU_ACTIVITY_LAUNCH_FRUITS, respMsg);
        }
    }
}
